<template>
  <div class="user-container">
    <!-- 1.顶部的盒子 -->
    <div class="info-box">
      <van-image
        round
        width="64"
        height="64"
        fit="cover"
        :src="userInfo.photo"
      />
      <h2>
        {{ userInfo.name }}<br /><van-tag
          color="#ffe1e1"
          text-color="#009dfb"
          >{{ userInfo.birthday }}</van-tag
        >
      </h2>
    </div>
    <!-- //2.用户操作的点击 -->
    <van-row>
      <van-col span="8">
        <van-icon name="newspaper-o" color="#99aaff" />
        <br />
        <span>我的作品</span>
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#ff4f31" />
        <br />
        <span>我的收藏</span>
      </van-col>
      <van-col span="8">
        <van-icon name="tosend" color="#ffa000" />
        <br />
        <span>阅读历史</span>
      </van-col>
    </van-row>
    <!-- //3.用户操作的cell -->
    <van-cell-group>
      <van-cell
        icon="location-o"
        title="编辑资料"
        is-link
        @click="$router.push({ path: '/edit' })"
      />
      <van-cell
        icon="chat-o"
        title="小智同学"
        is-link
        @click="$router.push('/student')"
      />
      <van-cell icon="setting-o" title="系统设置" is-link />
      <van-cell icon="warning-o" title="退出登录" is-link @click="logout" />
    </van-cell-group>
  </div>
</template>

<script>
//导入接口
import { getUserInfo } from './user.js'
import { removeToken } from '@/utils/token.js'

export default {
  name: 'user',
  data () {
    return {
      userInfo: {}
    }
  },
  created () {
    //1.请求个人详细信息
    getUserInfo().then(res => {
      console.log(res)
      //2.绑定数据
      this.userInfo = res.data.data
      //3.数据更新到vuex中
      this.$store.commit('updateUserInfo', res.data.data)
    })
  },
  methods: {
    logout () {
      //退出登录
      this.$dialog
        .confirm({
          title: '提示',
          message: '确定要退出登录吗'
        })
        .then(() => {
          console.log('清除token成功!')
          //用户点击确认触发
          //1.清楚token
          removeToken()
          //2.清除userInfo
          this.userInfo = {}
          //3.跳转登录页
          this.$router.push({ path: '/login' })
        })
        .catch(() => {
          //用户点击取消
        })
    }
  }
}
</script>

<style lang="less">
.user-container {
  //顶部的盒子
  .info-box {
    height: 100px;
    background-color: #0098f9;
    display: flex;
    //水平对齐
    // justify-content: center;
    //垂直对齐
    align-items: center;
  }
  h2 {
    color: #fff;
    font-size: 18px;
  }
  .van-image {
    padding: 0 15px;
  }
  //用户的操作链接
  .van-row {
    text-align: center;
    margin: 10px 0;
    span {
      font-size: 12px;
      display: block;
    }
  }
}
</style>
